package com.anzia.study_1.slice;

import com.anzia.study_1.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainAbilitySlice extends AbilitySlice {

	private Text text;

	// 接收并处理返回值的方法
	@Override
	protected void onResult(int requestCode, Intent resultIntent) {
		super.onResult(requestCode, resultIntent);

		if (requestCode == 1314) {
			String pwd = resultIntent.getStringParam("pwd");
			text.append(", " + pwd);
		}
	}

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		//ResourceManager resourceManager = this.getResourceManager();
		//try {
			// 获取系统资源的内容
			//String strTitle = resourceManager.getElement(ohos.global.systemres.ResourceTable.String_request_location_reminder_title).getString();

			// 获取元素资源的内容，元素存放位置在\resource\base\element\string.json中
			//String strCon = getResourceManager().getElement(ResourceTable.String_app_name).getString();

			//Text text = (Text) findComponentById(ResourceTable.Id_text);

			//int color = resourceManager.getElement(ResourceTable.Color_red).getColor();
			//text.setTextColor(new Color(color));

			//text.setText(strCon);

		//	Resource resource = resourceManager.getResource(ResourceTable.Profile_test);
		//	InputStreamReader reader = new InputStreamReader(resource, "utf-8");
		//	BufferedReader bufferedReader = new BufferedReader(reader);
		//	String lineText = null;
		//	while ((lineText = bufferedReader.readLine()) != null) {
		//		text.append("," + lineText);
		//	}
		//} catch (IOException e) {
		//	e.printStackTrace();
		//} catch (NotExistException e) {
		//	e.printStackTrace();
		//}

		//Text text = (Text) findComponentById(ResourceTable.Id_text);
		//
		//try {
		//	Resource resource = getResourceManager().getRawFileEntry("resources/rawfile/test1.txt").openRawFile();
		//	InputStreamReader reader = new InputStreamReader(resource, "utf-8");
		//	BufferedReader bufferedReader = new BufferedReader(reader);
		//	String str = null;
		//	while ((str = bufferedReader.readLine()) != null) {
		//		text.append(", " + str);
		//	}
		//
		//} catch (IOException e) {
		//	e.printStackTrace();
		//}

		text = (Text) findComponentById(ResourceTable.Id_text);
		text.setClickedListener(component -> {
			Intent intent1 = new Intent();
			intent1.setParam("user", "Anzia");
			//present(new AbilitySlice(), intent1);
			presentForResult(new AbilitySlice1(), intent1, 1314);
		});



		// 在这里获取到xml布局里的button
		Button button = (Button) findComponentById(ResourceTable.Id_button);

		// 给button按钮，添加点击的监听事件
		button.setClickedListener(component -> {
			// 点击了按钮后，会执行这里的代码，跳转页面
			// 跳转到的目的地
			Intent intent1 = new Intent();
			//Operation operation = new Intent.OperationBuilder()
			//		.withDeviceId("") // 方法确定跳转的目的设备，""表示本地
			//		.withBundleName("com.anzia.study_1") // 跳转的目标页面所在的应用
			//		.withAbilityName(".SecondAbility")
			//		.build();

			//intent1.setOperation(operation);

			intent1.setAction("abilityslice2");

			// 调用一个方法，实现我们从MainAbility跳转到SecondAbility
			startAbility(intent1);

		});
	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}
}
